package team.seekers.hula.system.resource.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.*;
import team.seekers.hula.system.resource.entity.dto.AliDemandDTO;

/**
 * date: 2022/4/24 10:37
 *
 * @author GH
 * QQ 1124813753
 */
public class AliVodUtil {

    private static final String ACCESS_KEY_ID = "LTAI5t9Mxa7mMsfjZbe4Wpfp";
    private static final String ACCESS_KEY_SECRET = "IGInHiB5ur2mp3kwJOscHaGKqfTq0z";
    private static final String REGION_ID = "cn-shanghai"; // 点播服务接入地域

    private static final DefaultAcsClient CLIENT = InitAliClient.init(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

    /** 获取音/视频上传地址和凭证*/
    public static AliDemandDTO uploadVideo(String title, String fileName) throws Exception {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(title);
        request.setFileName(fileName);
        CreateUploadVideoResponse response = CLIENT.getAcsResponse(request);

        AliDemandDTO demandDTO = AliDemandDTO.builder()
                .objectId(response.getVideoId())
                .uploadAddress(response.getUploadAddress())
                .uploadAuth(response.getUploadAuth())
                .build();
        return demandDTO;
    }

    /**
     * 刷新音/视频上传凭证
     */
    public static AliDemandDTO refreshUploadVideo(String videoId) throws Exception {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId(videoId);
        RefreshUploadVideoResponse response = CLIENT.getAcsResponse(request);

        AliDemandDTO demandDTO = AliDemandDTO.builder().uploadAuth(response.getUploadAuth()).build();
        return demandDTO;
    }

    /**获取图片上传地址和凭证*/
    public static AliDemandDTO uploadImage(String imgName, String imgExt) throws Exception {
        CreateUploadImageRequest request = new CreateUploadImageRequest();
        //设置图片类型
        request.setImageType("default");
        //设置图片扩展名
        request.setImageExt(imgExt);
        //设置图片标题
        request.setTitle(imgName);
        CreateUploadImageResponse response = CLIENT.getAcsResponse(request);

        AliDemandDTO demandDTO = AliDemandDTO.builder()
                .objectId(response.getImageId())
                .objectURL(response.getImageURL())
                .uploadAddress(response.getUploadAddress())
                .uploadAuth(response.getUploadAuth())
                .build();
        return demandDTO;
    }

    /**获取播放地址函数*/
    public static GetPlayInfoResponse getPlayInfo(String videoId) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        GetPlayInfoResponse response = CLIENT.getAcsResponse(request);

        return response;
    }

    /**获取播放凭证函数*/
    public static AliDemandDTO getVideoPlayAuth(String videoId) throws Exception {
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        request.setAuthInfoTimeout(3000L);
        GetVideoPlayAuthResponse response = CLIENT.getAcsResponse(request);

        AliDemandDTO demandDTO = AliDemandDTO.builder().playAuth(response.getPlayAuth()).build();
        return demandDTO;
    }

    /**
     * 获取辅助媒资(水印、字幕等)上传地址和凭证
     * @param bType 业务类型watermark：水印;subtitle：字幕;material：素材。
     */
    public static AliDemandDTO uploadAttachedMedia(String bType, String mediaName, String mediaExt) throws Exception {
        CreateUploadAttachedMediaRequest request = new CreateUploadAttachedMediaRequest();
        //设置业务类型
        request.setBusinessType(bType);
        //设置文件扩展名
        request.setMediaExt(mediaExt);
        //设置标题
        request.setTitle(mediaName);

        CreateUploadAttachedMediaResponse response = CLIENT.getAcsResponse(request);
        AliDemandDTO demandDTO = AliDemandDTO.builder()
                .objectId(response.getMediaId())
                .objectURL(response.getMediaURL())
                .uploadAddress(response.getUploadAddress())
                .uploadAuth(response.getUploadAuth())
                .build();
        return demandDTO;
    }
}
